package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.POJO.Permission;
import com.itheima.POJO.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface RoleMapper {
    @Select("select r.*  from t_role r ,t_user_role ur where r.id = ur.role_id and ur.user_id = #{userid}")
    Set<Role> findRoleByuserid(Integer userid);
    
    @Select("select * from t_role ")
    List<Role> findAllRole();

    @Select("<script>select * from t_role <where><if test='value !=null and value.length>0'> name like concat('%',#{value},'%')</if></where> </script>")
    Page<Role> getrole(String queryString);

    @Insert("insert into t_role values(null,#{name},#{keyword},#{description})")
    @Options(useGeneratedKeys = true,keyColumn = "id")
    void addRole(Role role);

    @Insert("insert into t_role_permission values(#{param1},#{param2})")
    void addRolePermission(Integer id, Integer checkitemId);

    @Select("select count(user_id) from t_user_role where role_id=#{param1}")
    Integer findUserCountById(Integer id);

    @Select("select count(menu_id) from t_role_menu where role_id=#{id} ")
    Integer findMenuCountById(Integer id);

    @Delete("delete from t_role_permission where role_id =#{param1}")
    void deleteRolePermissionByRoleId(Integer id);

    @Delete("delete from t_role where id=#{param1}")
    void deleteRoleByRoleId(Integer id);

    @Select("select * from t_role where id=#{id}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "keyword",column = "keyword"),
            @Result(property = "description",column = "description"),
            @Result(property = "permissions",column = "id",javaType = Set.class,many = @Many(select="com.itheima.dao.PermissionMapper.findPermissionsByRoleId")),

    })
    Role findRoleByRoleId(Integer id);

    @Update("update t_role set name=#{name},keyword=#{keyword},description=#{description} where id=#{id}")
    void updateRole(Role role);

}
